<?php

namespace App\Http\Controllers\Admin;
use App\DTOS\Admin\GameChannelDTO;
use App\Http\Controllers\Controller;
use App\Models\Game\ChannelPackage;
use App\Models\Game\GameChannel;
use App\Models\Game\HotVersion;
use App\Services\Game\GameChannelService;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;
// 游戏渠道管理
class GameChannelController extends Controller
{
    private GameChannelService $GameChannelService;

    /**
     * GameChannelController constructor.
     * @param GameChannelService $GameChannelService
     */
    public function __construct(GameChannelService $GameChannelService)
    {
        $this->GameChannelService = $GameChannelService;
    }

    public function paginate()
    {
        $paginate = $this->GameChannelService->paginate(new GameChannelDTO(request()->all()));
        $data = $paginate->items();
        $channelIds = collect($data)->pluck('id')->toArray();
        $channelPackages = ChannelPackage::query()->whereIn('game_channel_id',$channelIds)->get()->toArray();
        foreach ($data as &$datum) {
            $datum['gamePackageIds'] = collect($channelPackages)->where('game_channel_id',data_get($datum,'id'))
                ->pluck('game_package_id')->unique()->toArray();
        }
        $res = [];
        $res['code'] = 0;
        $res['msg'] = 'success';
        $res['data'] = $data;
        $res['count'] = $paginate->total();
        return ResponseUtil::json($res);
    }

    public function add()
    {
        $data = request()->only(['name','description','nick']);
        $data['created_at'] = date("Y-m-d H:i:s");
        GameChannel::query()->insert($data);
        return ResponseUtil::success();

    }
    public function edit($id)
    {
        $data = request()->only(['name','description','nick']);
        $data['updated_at'] = date("Y-m-d H:i:s");
        GameChannel::query()->where('id',$id)->update($data);
        return ResponseUtil::success();
    }
    public function detail($id)
    {
        $detail = GameChannel::query()->with('packages')->find($id);
        return ResponseUtil::success($detail);
    }
    public function delete($id)
    {
        GameChannel::query()->find($id)->delete();
        return ResponseUtil::success();
    }

    public function getByIds()
    {
        $ids = request()->get("ids",[]);
        if (empty($ids)) return ResponseUtil::success([]);
        $data = GameChannel::query()->with('packages')->whereIn('id',$ids)->get();
        return ResponseUtil::success($data);
    }
    public function getByIdsHV()
    {
      //  $hvmodel = HotVersion::query()->get();
      //  $pno = $hvmodel->pluck('package_no')->toArray();
        //切割后返回字符串
        //$pnos = implode(',', $pno);
        //切割后返回数组
        //$pnosArray = explode(',', $pnos);
        $ids = request()->get("ids",[]);
        if (empty($ids)) return ResponseUtil::success([]);
        $data = GameChannel::query()->with('packages')->whereIn('id', $ids)
            ->get();
        return ResponseUtil::success($data);
    }
}
